//
//  QAIMGameMangaer.m
//  AFNetworking
//
//  Created by 范浩云 on 2018/3/14.
//

#import "QAIMGameMangaer.h"

#define kEachKickErrorCode 6208
#define kUserSig @"eJxNjcFOg0AURf*Frca*GUCJO6wotVRioJqaJmTCDPigDAQGBBr-XSQYXbzNOffed9ZCL7hicVy2UkVqqIR2q4F2OWPkQipMUNQTTJjkpUx-btGsqpBHTEV6zf*1Gp5Hs5oYMQDgRjcMY5Gir7AWEUvUPEpM06RTZLGdqBss5SQoEJNQHeBPKizEXKEELIMA-f2H6YR3zmG9ebn3r912GNo0yx6gu1sH4accivFg4d7ap-ppdMB2ivjiUYb25sPeubI8rg593ZbMw-fjqhl67xRQx8enVzertvk2J8*Md-rb6Gtf39EwXFA_"

#define kSdkAppId       @"1400001533"

@interface QAIMGameMangaer()
<
TIMMessageListener,
TIMUserStatusListener
>
{
    dispatch_queue_t     _queue;
}
/// 群聊对象
@property (nonatomic, strong)  TIMConversation *grp_conversation;
/// 消息管理
@property (nonatomic, strong)  TIMManager * manager;

@property (nonatomic, strong) QAIMMessageModel *model;

@end

@implementation QAIMGameMangaer

-(void)dealloc
{
    NSLog(@"QAImManager dealloc");
}

static QAIMGameMangaer *_sharedInstance = nil;

+ (instancetype)shareGameManger
{
    static dispatch_once_t predicate;
    
    dispatch_once(&predicate, ^{
        _sharedInstance = [[QAIMGameMangaer alloc] init];
        [_sharedInstance initSDK];
    });
    
    return _sharedInstance;
    
}


#pragma ******************************************私有方法**********************************************

/**
 初始化SDK
 */
- (void)initSDK
{
    // 初始化SDK，设置sdkappid和accoutType，由腾讯云控制台分配
    TIMSdkConfig * config = [[TIMSdkConfig alloc] init];
//    config.sdkAppId =  [DefineQAIM_AppId intValue];
    config.sdkAppId =  [kSdkAppId intValue];

    config.accountType = @"7221";
    config.logLevel = TIM_LOG_NONE;

    [[TIMManager sharedInstance] initSdk:config];
    // 初始化用户参数配置，使用默认配置
    TIMUserConfig * userConfig = [[TIMUserConfig alloc] init];
    [[TIMManager sharedInstance] setUserConfig:userConfig];
    // 添加新消息监听器，实现onNewMessage:
    [[TIMManager sharedInstance] addMessageListener:self];
    
    [self initLoginUser];
}



/**
 登录自己测试用
 */
-(void)initLoginUser
{
    // 登陆
    TIMLoginParam * login_param = [[TIMLoginParam alloc ]init];
    login_param.identifier =@"haoyun";
    login_param.userSig = @"eJx1kEFPgzAYhu-8iqZno5QKsyYecBAmspGIRuHSIC3wzcAqFDI0-ncXNJGL7-V5kid5Pw2EEH6MkvO8KA5Dq7melMToGuEVs-DZH1YKBM81p52YMbk0TyM2pQtLHhV0kuellt1sWTazTtpCASFbDSX8CnV*mIZ2wXvxxufY-5Ueqhlu-af13fo9abr4xfOoiMxjkO2SsLzwIhbq8CNNGz2N8T65J89u9eqC79ZqBaA3aensClrfPgSx5wyV2U5bd9xcZUE-7v06s6tBpDeLpIbm5xViW4Qw6jgMG1-GNxZyWAg_";
    login_param.appidAt3rd = kSdkAppId;
    __weak typeof(self) weakSelf = self;
    [self login:login_param succ:^{
        
          NSLog(@"Login Succ");
    } fail:^(int code, NSString *msg) {
        
          NSLog(@"LoginFail;");
    }];
}



- (void)login:(TIMLoginParam *)param succ:(TIMLoginSucc)succ fail:(TIMFail)fail
{
    if (!param)
    {
        if (fail)
        {
            fail(-1,@"参数错误");
        }
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[TIMManager sharedInstance] login:param succ:^{
        
        NSLog(@"登录成功:%@ tinyid:%llu sig:%@", param.identifier, [[IMSdkInt sharedInstance] getTinyId], param.userSig);
        if (succ)
        {
            succ();
        }
    } fail:^(int code, NSString *msg) {
        
        if (code == kEachKickErrorCode)
        {
        }
        else
        {
            if (fail)
            {
                fail(code, msg);
            }
        }
    }];
}


//退出
- (void)logout:(TIMLoginSucc)succ fail:(TIMFail)fail
{
    __weak typeof(self) weakSelf = self;
    [[TIMManager sharedInstance] logout:^{
        if (succ)
        {
            succ();
        }
    } fail:^(int code, NSString *err) {
        if (fail)
        {
            fail(code, err);
        }
    }];
}



- (void)onNewMessage:(NSArray*)msgs
{

        for (TIMMessage *msg in msgs) {
            
            TIMConversationType type = msg.getConversation.getType;
            switch (type) {
                    case TIM_C2C: /// 单聊消息
                {
                    for (int i = 0; i<msg.elemCount;i++) {
                        TIMElem *timElem = [msg getElem:i];
                        if([timElem isKindOfClass:[TIMCustomElem class]])
                        {
                            TIMCustomElem * customElem = (TIMCustomElem *)timElem;
                            NSString *strs=[[NSString alloc] initWithData:customElem.data encoding:NSUTF8StringEncoding];
                            if (self.imMessageBlock)
                            {
                                self.imMessageBlock(strs);
                            }
                        }
                    }
                }
                    break;
                    case TIM_SYSTEM: /// 系统消息
                {
                    
                       NSLog(@"/// 系统消息");
                }
                    break;
              
                    case TIM_GROUP:  /// 群聊消息
                {
                    NSLog(@"/// 群聊消息");
                }
                    break;
                    
                default:
                    break;
            }
        }
}



@end
